package com.johnson.news.model.remote;

import android.content.Context;

import com.johnson.news.model.bean.AskMeNotice;
import com.johnson.news.model.bean.AskmeCell;
import com.johnson.news.model.bean.AskmeData;
import com.johnson.news.model.bean.BaseResponseData;
import com.johnson.news.model.bean.ContactItem;
import com.johnson.news.model.bean.ContinueView;
import com.johnson.news.model.bean.DataNewsCategory;
import com.johnson.news.model.bean.DataUserInfo;
import com.johnson.news.model.bean.DataWeiXinItem;
import com.johnson.news.model.bean.LaunchPic;
import com.johnson.news.model.bean.NewsContent;
import com.johnson.news.model.bean.NewsItem;
import com.johnson.news.model.bean.NewsReplyItem;
import com.johnson.news.model.bean.ReplyItem;
import com.johnson.news.model.bean.SearchResult;
import com.johnson.news.model.bean.StatusItem;
import com.johnson.news.model.bean.UpdateItem;
import com.johnson.news.model.bean.WeiboItem;
import com.johnson.news.model.util.NewsPreference;
import com.johnson.news.model.util.Transformer;
import com.johnson.news.util.Utils;

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import retrofit2.Retrofit;

public class RemoteDataSource {
    Retrofit retrofit;
    APIService apiService;

    public static class ApiResponse {
        public static final int result_ok = 200;
    }

    private static RemoteDataSource remoteDataSource;

    public static RemoteDataSource getRemoteDataSource() {
        if (remoteDataSource == null)
            remoteDataSource = new RemoteDataSource();
        return remoteDataSource;
    }


    public RemoteDataSource() {
        retrofit = RetrofitFactory.getRetrofit();
        apiService = retrofit.create(APIService.class);
    }

    public Observable<LaunchPic> getSplashInfo() {
        return apiService.getSplashInfo()
                .compose(Transformer.<LaunchPic>switchSchedulers());
    }

    int test = 0;
    public Observable<ArrayList<NewsItem>> getTopicNews(Context context, String mid, int pagesizes, int page) {
        return apiService.getTopicNews(mid, 1, pagesizes, page)
                .compose(Transformer.<ArrayList<NewsItem>>switchSchedulersAndLoading(context));

//        ArrayList<NewsItem> list = new ArrayList<>();
//        if(test++ % 2 == 0) {
//            NewsItem item = new NewsItem();
//            list.add(item);
//            item.setThumb("http://jyt.shaanxi.gov.cn//file//upload//202003//21//13-15-45-40-1.jpg");
//            item.setTitle("余德华任汉中职业技术学院党委书记");
//            item.setAddtime("1584767640");
//        }
//
//        return Observable.just(list)
//                .compose(Transformer.<ArrayList<NewsItem>>switchSchedulersAndLoading(context));

    }

    public Observable<ArrayList<DataNewsCategory>> getNewsCats() {
        return apiService.getNewsCats()
                .compose(Transformer.<ArrayList<DataNewsCategory>>switchSchedulers());
    }

    public Observable<ArrayList<NewsItem>> getNewsList(Context context, String mid, int catid, int istop, int pagesizes, int page) {
        return apiService.getNewsList(mid, catid, istop, pagesizes, page)
                .compose(Transformer.<ArrayList<NewsItem>>switchSchedulersAndLoading(context));
    }

    public Observable<NewsContent> getNewsDetail(Context context, String mid, String catid, String itemId) {
        return apiService.getNewsDetail(mid, catid, itemId)
                .compose(Transformer.<NewsContent>switchSchedulersAndLoading(context));
    }

    public Observable<ArrayList<NewsItem>> getNewsRelatedList(String mid, String catid, int pagesizes, int page) {
        return apiService.getNewsRelatedList(mid, catid, pagesizes, page)
                .compose(Transformer.<ArrayList<NewsItem>>switchSchedulers());
    }

    public Observable<ArrayList<NewsReplyItem>> getNewsReplies(String mid, String itemId, int pagesizes, int page) {
        return apiService.getNewsReplies(mid, itemId, pagesizes, page)
                .compose(Transformer.<ArrayList<NewsReplyItem>>switchSchedulers());
    }

    public Observable<BaseResponseData> newsComment(String mid, String itemid, String content, String ip) {
        return apiService.newsComment(mid, itemid, content, ip)
                .compose(Transformer.<BaseResponseData>switchSchedulers());
    }

    public Observable<BaseResponseData> newsLike(String itemid, String userName) {
        return apiService.newsLike(itemid, userName)
                .compose(Transformer.<BaseResponseData>switchSchedulers());
    }

    public Observable<ArrayList<WeiboItem>> getWeiboList(Context context) {
        return apiService.getWeiboList()
                .compose(Transformer.<ArrayList<WeiboItem>>switchSchedulersAndLoading(context));
    }
    public Observable<ContactItem> getContactUsItem(Context context) {
        return apiService.getContactUsItem()
                .compose(Transformer.<ContactItem>switchSchedulersAndLoading(context));
    }

    public Observable<BaseResponseData> getVerifyCodeOfRegister(String mob) {
        return apiService.getVerifyCodeOfRegister(mob)
                .compose(Transformer.<BaseResponseData>switchSchedulers());
    }

    public Observable<DataUserInfo> register(String mob, String yzm, String password, String channelid) {
        return apiService.register(mob, yzm, password, channelid, Utils.SYSTEM)
                .compose(Transformer.<DataUserInfo>switchSchedulers());
    }

    public Observable<DataUserInfo> login(String mob, String password, String channelid) {
        return apiService.login(mob, password, channelid, Utils.SYSTEM)
                .compose(Transformer.<DataUserInfo>switchSchedulers());

//        DataUserInfo dataUserInfo = new DataUserInfo();
//        dataUserInfo.phone = mob;
//        dataUserInfo.Status = 1;
//        return Observable.just(dataUserInfo)
//                .compose(Transformer.<DataUserInfo>switchSchedulers());
    }

    public Observable<DataUserInfo> modifyPwd(String mob, String token, String password) {
        return apiService.modifyPwd(mob, token, password)
                .compose(Transformer.<DataUserInfo>switchSchedulers());
    }

    public Observable<BaseResponseData> getVerifyCodeOfFindPwd(String mob) {
        return apiService.getVerifyCodeOfFindPwd(mob)
                .compose(Transformer.<BaseResponseData>switchSchedulers());
    }

    public Observable<DataUserInfo> findPwd(String mob, String yzm, String password) {
        return apiService.findPwd(mob, yzm, password)
                .compose(Transformer.<DataUserInfo>switchSchedulers());
    }

    public Observable<ArrayList<NewsItem>> search(Context context, String mid, String keyword, int pagesize, int page) {
        List<NameValuePair> list = new ArrayList();
        list.add(new BasicNameValuePair("mid", mid));
        list.add(new BasicNameValuePair("kw", !Utils.isEmptyString(keyword) ? keyword : ""));
        list.add(new BasicNameValuePair("pagesizes", String.valueOf(pagesize)));
        list.add(new BasicNameValuePair("page", String.valueOf(page)));
        String searchUrl = APIService.baseUrl + "appsearch.php" + "?" + URLEncodedUtils.format(list, "gb2312");
        return apiService.search(searchUrl)//, mid, keyword, pagesize, page
                .compose(Transformer.<ArrayList<NewsItem>>switchSchedulersAndLoading(context));
    }

    public Observable<AskmeData> getMyComplaint(Context context, int types, int pagesizes, int page) {
        return apiService.getMyComplaint(NewsPreference.getinstance(context).getToken(), types, pagesizes, page)
                .compose(Transformer.<AskmeData>switchSchedulersAndLoading(context));
//        ArrayList<AskmeCell> cells = new ArrayList<>();
//        AskmeData askmeData = new AskmeData();
//        askmeData.setStatus("1");
//        askmeData.setResult(cells);
//
//        for (int i = 0; i < 5; i++ ){
//            AskmeCell askmeCell = new AskmeCell();
//            askmeCell.setBt("标题---" + i);
//            askmeCell.setAddtime(1584686705);
//            askmeCell.setZhuangtai(i % 2 == 0 ? "已受理" : "受理中");
//            cells.add(askmeCell);
//        }
//        return Observable.just(askmeData)
//                .compose(Transformer.<AskmeData>switchSchedulersAndLoading(context));
    }

    public Observable<ReplyItem> getComplaintDetail(Context context, int itemid) {
        return apiService.getComplaintDetail(NewsPreference.getinstance(context).getToken(), itemid)
                .compose(Transformer.<ReplyItem>switchSchedulersAndLoading(context));
    }

    public Observable<AskMeNotice> getComplaintNotice(Context context, int noParams) {
        return apiService.getComplaintNotice(noParams)
                .compose(Transformer.<AskMeNotice>switchSchedulersAndLoading(context));
    }

    public Observable<BaseResponseData> postComplaint(Context context, String title,
                                         String content, String name,
                                         String email, String mob) {//, String address, String zhiye
        List<NameValuePair> list = new ArrayList();
        list.add(new BasicNameValuePair("token", NewsPreference.getinstance(context).getToken()));

        try {
            title = URLEncoder.encode(title, "gb2312");
            content = URLEncoder.encode(content, "gb2312");
            name = URLEncoder.encode(name, "gb2312");
////            email = URLEncoder.encode(email, "gb2312");
//            address = URLEncoder.encode(address, "gb2312");
//            zhiye = URLEncoder.encode(zhiye, "gb2312");
        } catch (UnsupportedEncodingException var16) {
            var16.printStackTrace();
        }

//        list.add(new BasicNameValuePair("title", title));
//        list.add(new BasicNameValuePair("content", content));
//        list.add(new BasicNameValuePair("name", name));
//        list.add(new BasicNameValuePair("email", email));
//        list.add(new BasicNameValuePair("mob", mob));
//        list.add(new BasicNameValuePair("ip", Utils.getPhoneDeviceId(context)));
//        String urlString = APIService.baseUrl + "appwwbwyzx.php?" + URLEncodedUtils.format(list, "gb2312");
        return apiService.postComplaint(NewsPreference.getinstance(context).getToken(),
                title, content, name, email, mob, Utils.getPhoneDeviceId(context))//, address, zhiye
                .compose(Transformer.<BaseResponseData>switchSchedulersAndLoading(context));
    }

    public Observable<UpdateItem> checkUpdate(int code, int apptype) {
//        List<NameValuePair> list = new ArrayList();
//        list.add(new BasicNameValuePair("version", version));
//        list.add(new BasicNameValuePair("apptype", apptype + ""));
//        String urlString = "http://www.snedu.gov.cn/mob/appupgrade.php" + "?" + URLEncodedUtils.format(list, "gb2312");//http://jyt.shaanxi.gov.cn/mob/appupgrade.php
        return apiService.checkUpdate(code, apptype)//
                .compose(Transformer.<UpdateItem>switchSchedulers());
    }

    public Observable<DataWeiXinItem> getOfficalWeiXin(Context context) {
        return apiService.getOfficalWeiXin()
                .compose(Transformer.<DataWeiXinItem>switchSchedulersAndLoading(context));
    }
}
